<template>
  <div>
    <yd-cell-group title="我的房间">
      <yd-cell-item arrow type="label">
        <select slot="right" @change="changeRoom" v-model="room">
          <option :key="index" :value="item.id" v-for="item, index in roomList ">{{item.housingName + " - " + item.roomInfo}}</option>
        </select>
      </yd-cell-item>
    </yd-cell-group>
    <yd-cell-group title="我的钥匙">
      <yd-cell-item v-for="item, key in keyList" :key="key" @click.native="openDoor(item)">
        <span slot="left">{{item.location}}</span>
        <yd-switch slot="right" v-model="item.switch"></yd-switch>
      </yd-cell-item>
    </yd-cell-group>
  </div>

</template>

<script>
    import http from "../../http"
    import storage from "../../store/storage.js"
    export default {
      name: "KeyList",
      data () {
        return {
          roomList: [],
          keyList: [],
          room: "",
          flag: false
        }
      },
      created: function () {
        this.init()
      },
      methods: {
        init: function () {
          const housingId = storage.getObj("housingId").id;
          http.get("/communityApps/personalCenter/rooms?housingId=" + housingId).then(res => {
            if (res.data.code == 20000) {
              this.roomList = res.data.data;
              if (this.roomList.length > 0) {
                const room = this.roomList[0]
                this.room = room.id
                this.getKeys(room.id)
              }
            } else {
              this.$dialog.toast({
                mes: res.data.message,
                timeout: 1500
              });
            }
          });
        },
        getKeys: function (roomId) {
          http.get("/communityApps/acs/keys/room/" + roomId).then(res => {
            if (res.data.code == 20000) {
              const data = res.data.data;
              for (const item of data) {
                item.switch = false
              }
              this.keyList = data;

            } else {
              this.$dialog.toast({
                mes: res.data.message,
                timeout: 1500
              });
            }
          });
        },
        changeRoom: function () {
          this.keyList = []
          this.getKeys(this.room)
        },
        openDoor: function (item) {
          if (item.switch == true) {
            return
          }
          item.switch = true
          setTimeout(function () {
            item.switch = false
          }, 1000)
          http.get('/communityApps/acs/open/door/' + item.dtuMac).then(resp => {
            console.log(resp)
            if (resp.data.code === 20000) {
              if (resp.data.data === true) {
                this.$dialog.toast({
                  mes: '开门成功',
                  timeout: 2000,
                  icon: 'success',
                });

              } else {
                this.$dialog.toast({
                  mes: '开门失败，请联系管理员',
                  timeout: 2000,
                  icon: 'error'
                });
              }
            } else {
              this.$dialog.toast({
                mes: resp.data.message,
                timeout: 2000,
                icon: 'error'
              });
            }
          })
        }
      }

    }
</script>

<style scoped>

</style>
